<?php

namespace app\api\controller;

use think\Db;

class Match extends Common
{
    public function lover()
    {
        $records = Db::name('lover_match_records')->where(['user_id' => $this->member['id']])->order('id desc')->find();

        if ($records) {
            $time = time() - $records['update_time'];
            if ($time < 3) {
                $data = json_decode($records['data'], true);
                return $this->pass($data);
            }
        }

        $sex = $this->member['sex'] == '男' ? '女' : '男';
        $types = $this->member['sex'] == '男' ? 1 : 2;
        $types_another = $this->member['sex'] == '男' ? 2 : 1;

        $where = ['sex' => $sex];

        if ($this->member['age']) {
            $min = $this->member['age'] - 20;
            $max = $this->member['age'] + 20;
            $where['age'] = ['between', [$min, $max]];
        }

        $user = Db::name('user')->where($where)->orderRaw('rand()')->find();

        if ($user) {
            $template = Db::name('lover_match_template')->where(['types' => $types])->orderRaw('rand()')->find();

            $template_another = Db::name('lover_match_template_another')->where(['types' => $types_another])->orderRaw('rand()')->find();

            $template_quality = Db::name('lover_match_template_quality')->where(['types' => $types])->orderRaw('rand()')->find();

            $rate = mt_rand(900, 999);

            $data = [
                'user_id' => $this->member['id'],
                'avatar' => changeUrl($this->member['head_100']),
                'name' => $this->member['name'],
                'another_id' => $user['id'],
                'another_avatar' => changeUrl($user['head_100']),
                'another_name' => $user['name'],
                'rate' => number_format($rate / 10, 1),
                'face' => $template['face'],
                'likeable' => $template_quality['likeable'],
                'description' => $template_quality['description'],
                'quality' => [
                    ['title' => $template_quality['quality_1'], 'rate' => $template_quality['quality_1_rate']],
                    ['title' => $template_quality['quality_2'], 'rate' => $template_quality['quality_2_rate']],
                    ['title' => $template_quality['quality_3'], 'rate' => $template_quality['quality_3_rate']],
                ],
                'sign' => $template['sign'],
                'another_face' => $template_another['face'],
                'another_specialty' => $template_another['specialty'],
                'another_dna' => $template_another['dna'],
                'another_image' => $template_another['image'],
                'outcome' => $template['outcome'],
            ];

            $records = Db::name('lover_match_records')->where(['user_id' => $this->member['id'], 'to_user' => $user['id']])->find();

            if ($records) {
                Db::name('lover_match_records')->where(['id' => $records['id']])->update([
                    'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
                    'update_time' => time()
                ]);
            } else {
                Db::name('lover_match_records')->insert([
                    'user_id' => $this->member['id'],
                    'to_user' => $user['id'],
                    'data' => json_encode($data, JSON_UNESCAPED_UNICODE),
                    'create_time' => time(),
                    'update_time' => time(),
                ]);

                /*if ($this->member['volley']) {
                    $gift = Db::name('gift')->field('id, price')->where(['types' => 1])->order('price asc')->find();

                    if ($gift && $this->member['volley'] >= $gift['price']) {
                        $url = chatUrl();

                        $datas = [
                            'userid' => $this->member['id'],
                            'touserid' => $user['id'],
                            'type' => 8,
                            'id' => $gift['id'],
                            'num' => 1,
                        ];
                        curl($url, json_encode($datas, JSON_UNESCAPED_UNICODE));
                    }
                }*/

                $types = $this->member['sex'] == '男' ? 1 : 2;
                $contents = Db::name('privatechat_default')->where(['types' => $types, 'channel' => 2, 'user_id' => ['in', [0, $this->member['id']]]])->column('content');

                if ($contents) {
                    $url = chatUrl();
                    $rand = mt_rand(0,count($contents) - 1);
                    $datas = [
                        'userid' => $this->member['id'],
                        'touserid' => $user['id'],
                        'content' => $contents[$rand],
                        'timing' => 1,
                        'greet' => 1,
                        'match' => 1,
                    ];

                    curl($url, json_encode($datas, JSON_UNESCAPED_UNICODE));
                }
            }

            return $this->pass($data);
        } else {
            return $this->return['match.error.empty'];
        }
    }
    
    public function avatar()
    {
        $sex = $this->member['sex'] == '男' ? '女' : '男';
    
        $avatar = Db::name('user')->where(['sex' => $sex])->limit(60)->orderRaw('rand()')->column('head_100');
    
        foreach ($avatar as $key => $item) {
            $avatar[$key] = changeUrl($item);
        }
        
        return $this->pass($avatar);
    }
}
